<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="交流输入线电压 " prop="acIn">
      <el-input v-model="dataForm.acIn" placeholder="交流输入线电压 "></el-input>
    </el-form-item>
    <el-form-item label="交流输出相电压" prop="acOut">
      <el-input v-model="dataForm.acOut" placeholder="交流输出相电压"></el-input>
    </el-form-item>
    <el-form-item label="交流输出电流" prop="iOut">
      <el-input v-model="dataForm.iOut" placeholder="交流输出电流"></el-input>
    </el-form-item>
    <el-form-item label="输出频率" prop="outFrequency">
      <el-input v-model="dataForm.outFrequency" placeholder="输出频率"></el-input>
    </el-form-item>
    <el-form-item label="输出过载" prop="outOverload">
      <el-input v-model="dataForm.outOverload" placeholder="输出过载"></el-input>
    </el-form-item>
    <el-form-item label="createTime" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="createTime"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getObj, addObj, putObj} from '@/api/hik/cdcups'

    export default {
    data () {
      return {
        visible: false,
        canSubmit: false,
        dataForm: {
          indexCode: 0,
          acIn: '',

          acOut: '',

          iOut: '',

          outFrequency: '',

          outOverload: '',

          createTime: '',

        },
        dataRule: {
          acIn: [
            { required: true, message: '交流输入线电压 不能为空', trigger: 'blur' }
          ],

          acOut: [
            { required: true, message: '交流输出相电压不能为空', trigger: 'blur' }
          ],

          iOut: [
            { required: true, message: '交流输出电流不能为空', trigger: 'blur' }
          ],

          outFrequency: [
            { required: true, message: '输出频率不能为空', trigger: 'blur' }
          ],

          outOverload: [
            { required: true, message: '输出过载不能为空', trigger: 'blur' }
          ],

          createTime: [
            { required: true, message: 'createTime不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.indexCode = id || 0;
        this.visible = true;
        this.canSubmit = true;
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.indexCode) {
            getObj(this.dataForm.indexCode).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.canSubmit = false;
            if (this.dataForm.indexCode) {
                putObj(this.dataForm).then(data => {
                    this.$notify.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            } else {
                addObj(this.dataForm).then(data => {
                    this.$notify.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                }).catch(() => {
                    this.canSubmit = true;
                });
            }
          }
        })
      }
    }
  }
</script>
